package com.jumei.flume.processor.rules;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jumei.flume.bo.SensorMessage;
import com.jumei.flume.common.PageUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;

import static com.jumei.flume.common.StringUtils.safe4Param;

public class GeneralRules {

	/**
	 * 其他事件
	 */
	public final static HashMap<String, TypeResolver> resolvers = new HashMap<String, TypeResolver>() {
		{
			//2
			put("点击push", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("push_click", "");
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("push_click");
				}

			});

			//3
			put("启动页广告", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					String selllabel = PageUtils.getLabelName(safe4Param(record.getMaterial_link(), ""));
					return new Type("splash_ads_click", selllabel);
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("splash_ads_click");
				}

			});

			//4
			put("首页弹层", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("click_bottom_link", "");
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("promocard_dialog")
							&& "click_bottom_link".equals(record.getAction());
				}

			});

			//5
			put("用户中心-收藏列表", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("usercenter_1", "favorite_list");
				}

				public boolean filter(SensorMessage record) {
					ArrayList<String> temp = Lists.newArrayList("收藏店铺", "收藏商品");
					return record.getEvent().equals("click_material") && temp.contains(record.getMaterial_name());
				}

			});

			//6
			put("用户中心-浏览记录", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("usercenter_1", "browse_history");
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material")
							&& safe4Param(record.getMaterial_name(), "").equals("浏览记录");
				}
			});

			//7
			put("用户中心-订单列表", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("usercenter_1", "order_list");
				}

				public boolean filter(SensorMessage record) {
					ArrayList<String> temp = Lists.newArrayList("我的订单", "待付款", "待发货", "待收货", "待评价");
					return record.getEvent().equals("click_material") && temp.contains(record.getMaterial_name());
				}

			});

			//8
			put("用户中心-活动日历", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("usercenter_1", "act_calendar");
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material")
							&& safe4Param(record.getMaterial_name(), "").equals("活动日历")
							&& safe4Param(record.getMaterial_id(), "").equals("act_calendar");
				}

			});

			//9
			put("用户中心-心愿单", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("usercenter_1", "wish_deal");
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material") && "心愿单".equals(record.getMaterial_name())
							&& "wish_deal".equals(record.getMaterial_id());
				}

			});

			//10
			put("用户中心-其他", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("usercenter_1", record.getMaterial_id());
				}

				public boolean filter(SensorMessage record) {
					ArrayList<String> position = Lists.newArrayList("Activity","Store","Detail");
					return record.getEvent().equals("click_material") 
							&& safe4Param(record.getCurrent_page_url(), "").contains("jumeimall://page/account") 
							&& ! position.contains(record.getMaterial_page_type());
				}

				public int rank() {
					return 9999;
				}
			});

			//11
			put("用户中心-常购清单", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("usercenter_2", "purchasing_list");
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("purchasing_list_entrance_click");
				}

			});

			//12
			put("购物车-大家还买了", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("shop_cart", record.getMaterial_position());
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material") && "cart_show".equals(record.getMaterial_page())
							&& "app_cart_otherbuy".equals(record.getMaterial_position());
				}

			});

			//13
			put("购物车-促销凑单", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("shop_cart", record.getType());
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material") && "cart_show".equals(record.getMaterial_page())
							&& "app_cart_product".equals(record.getMaterial_position())
							&& "promotion".equals(record.getType());
				}
			});

			//14
			put("购物车-包邮凑单", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("shop_cart", record.getType());
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material") && "cart_show".equals(record.getMaterial_page())
							&& "app_cart_product".equals(record.getMaterial_position())
							&& "shipping".equals(record.getType());
				}

			});

			//15
			put("购物车-为您推荐", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("shop_cart", record.getMaterial_position());
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material") && "cart_show".equals(record.getMaterial_page())
							&& "app_cart_recommend".equals(record.getMaterial_position());
				}

			});

			//16
			put("购物车-失效后相似推荐", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("shop_cart", record.getMaterial_position());
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material") && "cart_show".equals(record.getMaterial_page())
							&& "app_cart_show_invalid_similar".equals(record.getMaterial_position());
				}

			});

			//17
			//https://echo.int.jumei.com/issues/157689
//			put("购物车-其他", new TypeResolver() {
//
//				@Override
//				public Type resolve(SensorMessage record) {
//					return new Type("shop_cart", record.getMaterial_position());
//				}
//
//				public boolean filter(SensorMessage record) {
//					ArrayList<String> position = Lists.newArrayList("app_cart_product", "app_cart_show_invalid_similar",
//							"app_cart_recommend", "app_cart_otherbuy");
//					return record.getEvent().equals("click_material") && "cart_show".equals(record.getMaterial_page())
//							&& !position.contains(record.getMaterial_position());
//				}
//
//				public int rank() {
//					return 9998;
//				}
//			});

			//18
			//https://echo.int.jumei.com/issues/157656
			//删除此规则
//			put("逛逛短视频", new TypeResolver() {
//
//				@Override
//				public Type resolve(SensorMessage record) {
//					return new Type("app_tab_vedio", "");
//				}
//
//				public boolean filter(SensorMessage record) {
//					if (Strings.isNullOrEmpty(record.getMaterial_name())) {
//						return false;
//					}
//
//					return record.getEvent().equals("app_tab")
//							&& (record.getMaterial_name().contains("逛逛") || record.getMaterial_name().contains("视频"))
//							&& Strings.isNullOrEmpty(record.getAction());
//				}
//
//			});

			//19
			put("专场", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("activity", record.getActivity_label_id());
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material")
							&& "Activity".equals(record.getMaterial_page_type());
				}

			});

			//20
			put("店铺", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("store", record.getMaterial_item_id());
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material") && "Store".equals(record.getMaterial_page_type());
				}

			});

			//21
			put("商品（排除凑团详情页）", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("detail", record.getMaterial_item_id());
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material")
							&& "Detail".equals(record.getMaterial_page_type());
				}

			});
			
			//22
			put("凑团详情页", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("coutuan_detail", record.getMaterial_item_id());
				}

				public boolean filter(SensorMessage record) {
					String url = record.getMaterial_link();
					if(Strings.isNullOrEmpty(url)) {
						return false;
					}
					
					
					Pattern p = Pattern.compile(
							"jumeimall://page/grouponproduct|http.*jumei.com/(yiqituan/detail[^Secret]|s/coutuan/)");
					boolean matches = Boolean.FALSE;
					if (p.matcher(url).find()) {
						matches = true;
					}
					
					ArrayList<String> cList = Lists.newArrayList("com.jm.android.jumei.GroupDetailActivity","JMGrouponDetailViewController");
					if(cList.contains(url)) {
						matches = true;
					}
					
					return record.getEvent().equals("click_material") && matches;
				}

			});
			
			//23
			put("加入购物车（h5，普通，提前）", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					HashMap<String, String> r = Maps.newHashMap();
					r.put("item_id", record.getItem_id());
					r.put("sku_no", record.getSelected_sku());
					r.put("os", record.getOs());
					r.put("app_version", record.getApp_version());
					r.put("uid", record.getDistinctId());
					
					return new Type("add_cart", JSON.toJSONString(r));
				}

				public boolean filter(SensorMessage record) {
					ArrayList<String> temp = Lists.newArrayList("h5_add_cart", "click_add_shopcar", "click_wish_add_shopcar");
					return temp.contains(record.getEvent());
				}

			});
			
			//24
			put("直接结算", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					HashMap<String, String> r = Maps.newHashMap();
					r.put("item_id", record.getItem_id());
					r.put("sku_no", record.getSelected_sku());
					r.put("os", record.getOs());
					r.put("app_version", record.getApp_version());
					r.put("uid", record.getDistinctId());
					
					return new Type("add_submit", JSON.toJSONString(r));
				}

				public boolean filter(SensorMessage record) {
					ArrayList<String> temp = Lists.newArrayList("click_add_submit");
					return temp.contains(record.getEvent());
				}

			});
			
			//25  参团（详情页单独买/开团，现状页参团）
			put("参团（详情页单独买/开团，现状页参团）", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					HashMap<String, String> r = Maps.newHashMap();
					r.put("item_id", record.getItem_id());
					r.put("sku_no", record.getSelected_sku());
					r.put("os", record.getOs());
					r.put("app_version", record.getApp_version());
					r.put("uid", record.getDistinctId());
					
					return new Type("add_coutuan", JSON.toJSONString(r));
				}

				public boolean filter(SensorMessage record) {
					ArrayList<String> temp = Lists.newArrayList("click_button_coutuan");
					return temp.contains(record.getEvent());
				}

			});
			
			//26 菜单
			put("菜单", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("top_menu", record.getMaterial_id());
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("click_material")
							&& "top_menu".equals(record.getCard_type());
				}

			});
			
			//27 购物车-已加购商品
			put("购物车-已加购商品", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("shop_cart", "app_cart_product");
				}

				public boolean filter(SensorMessage record) {
					return record.getEvent().equals("app_cart_product_click");
				}

			});

			//jumei echo上的需求编号#166090
			put("曝光事件兼容", new TypeResolver() {

				@Override
				public Type resolve(SensorMessage record) {
					return new Type("channel_level_1", record.getMaterial_page());
				}

				public boolean filter(SensorMessage record) {

					String material_page = record.getMaterial_page();
					ArrayList<String> material_pageList = Lists.newArrayList("home_main","global","baby_0527","luxury_pindao","apparel","accessory","jiajushuma_pindao","food_global");

					return record.getEvent().equals("view_material")&& !"top_tab".equals(record.getCard_type())&&material_pageList.contains(material_page);
				}

			});
			
		}
	};

}
